package com.zhifei.web.controller.employee;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhifei.common.utils.Constant;
import com.zhifei.common.utils.DateUtils;
import com.zhifei.common.utils.DownloadTemplateUtil;
import com.zhifei.common.utils.Result;
import com.zhifei.common.utils.excel.ExcelUtils;
import com.zhifei.system.domain.EmployeeMonthEntity;
import com.zhifei.system.domain.dto.UpdateEmployeeMonthDTO;
import com.zhifei.system.domain.vo.*;
import com.zhifei.system.service.*;
import com.zhifei.system.util.PageUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/employee")
@Api(tags="外派人员管理")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private EmployeeMonthService employeeMonthService;
    @Autowired
    private EmployeeSalaryService employeeSalaryService;
    @Autowired
    private EmployeeArchivesService employeeArchivesService;
    @Autowired
    private EmployeeGrowRecordService employeeGrowRecordService;
    @Autowired
    private DingUserArchivesService dingUserArchivesService;

    @GetMapping("page")
    @ApiOperation("分页首页列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int", defaultValue = "1") ,
            @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int", defaultValue = "10"),
            @ApiImplicitParam(name = "customerName", value = "客户名称", paramType = "query", required = false, dataType = "String"),
            @ApiImplicitParam(name = "employeeStatus", value = "状态（1-在项；2-离职；3-在职出项；4-在职待定；）", paramType = "query", required = false, dataType = "int"),
            @ApiImplicitParam(name = "userPost", value = "岗位", paramType = "query", required = false, dataType = "String"),
            @ApiImplicitParam(name = "userRank", value = "职级（初级；初中级；中级；中高级；高级；）", paramType = "query", required = false, dataType = "String"),
            @ApiImplicitParam(name = "admissionStartDate", value = "入场开始时间", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "admissionEndDate", value = "入场结束时间", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "leaveProjectStartDate", value = "出项开始时间", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "leaveProjectEndDate", value = "出项结束时间", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "serviceManager", value = "业务经理", paramType = "query", required = false, dataType = "String"),
            @ApiImplicitParam(name = "recruitHr", value = "招聘专员", paramType = "query", required = false, dataType = "String"),
            @ApiImplicitParam(name = "leaveProjectStatus", value = "是否快要离项（0-否；1-是；）", paramType = "query", required = false, dataType = "int"),
            @ApiImplicitParam(name = "name", value = "名称", paramType = "query", required = false, dataType = "String"),
            @ApiImplicitParam(name = "orderNo", value = "所属订单", paramType = "query", required = false, dataType = "String"),
            @ApiImplicitParam(name = "sortField", value = "排序字段（admission_date-入场时间；leave_project_date-出项时间；project_cycle-项目周期；quote_month_pay-报价月薪；month_pay-薪资；social_security-社保公积金；gross_profit-毛利；gross_profit_rate-毛利率；）", paramType = "query", required = false, dataType = "String"),
            @ApiImplicitParam(name = "sortType", value = "排序方式（asc-升序；desc-降序；）", paramType = "query", required = false, dataType = "String")
    })
    public Result<Page<EmployeeVo>> page(@ApiIgnore @RequestParam Map<String, Object> params){
        PageUtils page = employeeService.queryPage(params);
        return Result.success(page);
    }

    @GetMapping("pageMonth")
    @ApiOperation("分页每月实际详情列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int", defaultValue = "1") ,
            @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int", defaultValue = "10"),
            @ApiImplicitParam(name = "jobNum", value = "工号", paramType = "query", required = false, dataType = "String"),
            @ApiImplicitParam(name = "fullName", value = "姓名", paramType = "query", required = false, dataType = "String"),
            @ApiImplicitParam(name = "customerName", value = "客户名称", paramType = "query", required = false, dataType = "String"),
            @ApiImplicitParam(name = "projectName", value = "项目名称", paramType = "query", required = false, dataType = "String"),
            @ApiImplicitParam(name = "serviceManager", value = "业务经理", paramType = "query", required = false, dataType = "String"),
            @ApiImplicitParam(name = "recruitHr", value = "招聘专员", paramType = "query", required = false, dataType = "String"),
            @ApiImplicitParam(name = "monthStartDate", value = "月份开始时间", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "monthEndDate", value = "月份结束时间", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "collectionStartDate", value = "回款开始时间", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "collectionEndDate", value = "回款结束时间", required = false, paramType = "query", dataType = "String")
    })
    public Result<Page<EmployeeMonthVo>> pageMonth(@ApiIgnore @RequestParam Map<String, Object> params){
        PageUtils page = employeeMonthService.pageMonth(params);
        return Result.success(page);
    }

    @GetMapping("/export")
    @ApiOperation("导出")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "jobNum", value = "工号", paramType = "query", required = false, dataType = "String"),
            @ApiImplicitParam(name = "fullName", value = "姓名", paramType = "query", required = false, dataType = "String"),
            @ApiImplicitParam(name = "customerName", value = "客户名称", paramType = "query", required = false, dataType = "String"),
            @ApiImplicitParam(name = "projectName", value = "项目名称", paramType = "query", required = false, dataType = "String"),
            @ApiImplicitParam(name = "serviceManager", value = "业务经理", paramType = "query", required = false, dataType = "String"),
            @ApiImplicitParam(name = "recruitHr", value = "招聘专员", paramType = "query", required = false, dataType = "String"),
            @ApiImplicitParam(name = "monthStartDate", value = "月份开始时间", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "monthEndDate", value = "月份结束时间", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "collectionStartDate", value = "回款开始时间", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "collectionEndDate", value = "回款结束时间", required = false, paramType = "query", dataType = "String")
    })
    public void export(HttpServletRequest request, HttpServletResponse response, @ApiIgnore @RequestParam Map<String, Object> params){
        List<EmployeeMonthExcelVo> employeeMonthExcelVoList = employeeMonthService.listMonth(params);
        ExcelUtils.downloadExcel(request,response,employeeMonthExcelVoList,EmployeeMonthExcelVo.class,"每月实际详情","每月实际详情",null);
    }

    @PostMapping("/import")
    @ApiOperation("导入")
    public Result importMonth(MultipartFile file) {
        return employeeMonthService.importMonth(file);
    }

    @GetMapping("getMonthSummary")
    @ApiOperation("每月实际详情汇总")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "jobNum", value = "工号", paramType = "query", required = false, dataType = "String"),
            @ApiImplicitParam(name = "fullName", value = "姓名", paramType = "query", required = false, dataType = "String"),
            @ApiImplicitParam(name = "customerName", value = "客户名称", paramType = "query", required = false, dataType = "String"),
            @ApiImplicitParam(name = "projectName", value = "项目名称", paramType = "query", required = false, dataType = "String"),
            @ApiImplicitParam(name = "serviceManager", value = "业务经理", paramType = "query", required = false, dataType = "String"),
            @ApiImplicitParam(name = "recruitHr", value = "招聘专员", paramType = "query", required = false, dataType = "String"),
            @ApiImplicitParam(name = "monthStartDate", value = "月份开始时间", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "monthEndDate", value = "月份结束时间", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "collectionStartDate", value = "回款开始时间", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "collectionEndDate", value = "回款结束时间", required = false, paramType = "query", dataType = "String")
    })
    public Result<EmployeeMonthSummaryVo> getMonthSummary(@ApiIgnore @RequestParam Map<String, Object> params){
        EmployeeMonthSummaryVo vo = employeeMonthService.getMonthSummary(params);
        return Result.success(vo);
    }

    @PostMapping("/updateMonth")
    @ApiOperation("编辑每月实际详情")
    public Result<EmployeeMonthEntity> updateMonth(@RequestBody @Valid UpdateEmployeeMonthDTO dto, BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            return Result.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return employeeMonthService.updateMonth(dto);
    }

    @GetMapping("pageSalary")
    @ApiOperation("分页薪资发放记录明细列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int", defaultValue = "1") ,
            @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int", defaultValue = "10"),
            @ApiImplicitParam(name = "month", value = "月份", paramType = "query", required = false, dataType = "String"),
            @ApiImplicitParam(name = "jobNum", value = "工号", paramType = "query", required = false, dataType = "String")
    })
    public Result<Page<EmployeeSalaryVo>> pageSalary(@ApiIgnore @RequestParam Map<String, Object> params){
        PageUtils page = employeeSalaryService.pageSalary(params);
        return Result.success(page);
    }

    @GetMapping("pageGanttChart")
    @ApiOperation("分页甘特图列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int", defaultValue = "1") ,
            @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int", defaultValue = "10"),
            @ApiImplicitParam(name = "customerName", value = "客户名称", paramType = "query", required = false, dataType = "String"),
            @ApiImplicitParam(name = "employeeStatus", value = "状态（1-在项；2-离职；3-在职出项；4-在职待定；）", paramType = "query", required = false, dataType = "int"),
            @ApiImplicitParam(name = "userPost", value = "岗位", paramType = "query", required = false, dataType = "String"),
            @ApiImplicitParam(name = "userRank", value = "职级（初级；初中级；中级；中高级；高级；）", paramType = "query", required = false, dataType = "String"),
            @ApiImplicitParam(name = "admissionStartDate", value = "入场开始时间", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "admissionEndDate", value = "入场结束时间", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "leaveProjectStartDate", value = "出项开始时间", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "leaveProjectEndDate", value = "出项结束时间", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "serviceManager", value = "业务经理", paramType = "query", required = false, dataType = "String"),
            @ApiImplicitParam(name = "recruitHr", value = "招聘专员", paramType = "query", required = false, dataType = "String"),
            @ApiImplicitParam(name = "leaveProjectStatus", value = "是否快要离项（0-否；1-是；）", paramType = "query", required = false, dataType = "int"),
            @ApiImplicitParam(name = "name", value = "名称", paramType = "query", required = false, dataType = "String"),
            @ApiImplicitParam(name = "orderNo", value = "所属订单", paramType = "query", required = false, dataType = "String")
    })
    public Result<EmployeeGanttChartPageVo> pageGanttChart(@ApiIgnore @RequestParam Map<String, Object> params){
        EmployeeGanttChartPageVo page = employeeService.pageGanttChart(params);
        return Result.success(page);
    }

    @PostMapping("/importSalary")
    @ApiOperation("导入工资表")
    public Result importSalary(@RequestParam("file")MultipartFile file,@RequestParam("month")String month) {
        return employeeSalaryService.importSalary(file,month);
    }

    @GetMapping("/downloadSalaryTemplate")
    @ApiOperation("工资表模版下载")
    public void downloadSalaryTemplate(HttpServletResponse response){
        try {
            DownloadTemplateUtil.download(response, "工资表模版");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @GetMapping("cacl")
    @ApiOperation("测试计算值")
    public Result cacl(String id){
        employeeService.cacl(id);
        return Result.success("测试成功");
    }

    @GetMapping("test")
    @ApiOperation("测试")
    public Result test(){
        Date firstDayOfNowMonth = DateUtils.getFirstDayOfNowMonth();
        Date lastDayOfNowMonth = DateUtils.getLastDayOfNowMonth();
//        employeeArchivesService.generateArchives(firstDayOfNowMonth,lastDayOfNowMonth);
        dingUserArchivesService.generateArchives(firstDayOfNowMonth,lastDayOfNowMonth);
        return Result.success("测试成功");
    }

    @GetMapping("pageArchives")
    @ApiOperation("分页档案记录列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int", defaultValue = "1") ,
            @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int", defaultValue = "10"),
            @ApiImplicitParam(name = "customerName", value = "客户名称", paramType = "query", required = false, dataType = "String"),
            @ApiImplicitParam(name = "employeeStatus", value = "状态（1-在项；2-离职；3-在职出项；4-在职待定；）", paramType = "query", required = false, dataType = "int"),
            @ApiImplicitParam(name = "userPost", value = "岗位", paramType = "query", required = false, dataType = "String"),
            @ApiImplicitParam(name = "userRank", value = "职级（初级；初中级；中级；中高级；高级；）", paramType = "query", required = false, dataType = "String"),
            @ApiImplicitParam(name = "serviceManager", value = "业务经理", paramType = "query", required = false, dataType = "String"),
            @ApiImplicitParam(name = "recruitHr", value = "招聘专员", paramType = "query", required = false, dataType = "String"),
            @ApiImplicitParam(name = "name", value = "名称", paramType = "query", required = false, dataType = "String"),
            @ApiImplicitParam(name = "orderNo", value = "所属订单", paramType = "query", required = false, dataType = "String"),
            @ApiImplicitParam(name = "monthStartDate", value = "月份开始时间", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "monthEndDate", value = "月份结束时间", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "sortField", value = "排序字段（admission_date-入场时间；leave_project_date-出项时间；project_cycle-项目周期；quote_month_pay-报价月薪；month_pay-薪资；social_security-社保公积金；gross_profit-毛利；gross_profit_rate-毛利率；）", paramType = "query", required = false, dataType = "String"),
            @ApiImplicitParam(name = "sortType", value = "排序方式（asc-升序；desc-降序；）", paramType = "query", required = false, dataType = "String")
    })
    public Result<Page<EmployeeArchivesVo>> pageArchives(@ApiIgnore @RequestParam Map<String, Object> params){
        PageUtils page = employeeArchivesService.queryPage(params);
        return Result.success(page);
    }

    @GetMapping("listGrowRecord")
    @ApiOperation("成长记录列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "jobNum", value = "工号", paramType = "query", required = false, dataType = "String")
    })
    public Result<List<EmployeeGrowRecordVo>> listGrowRecord(@ApiIgnore @RequestParam Map<String, Object> params){
        List<EmployeeGrowRecordVo> list = employeeGrowRecordService.list(params);
        return Result.success(list);
    }

}


